/**
 * Copyright(C): CETC-32
 * @Description: 数据管理目录接口实现
 * @author: youqing
 * @version: 1.0
 * @date: 2020/9/11 10:55
 * 更改描述：
 */
package com.cetc32.dh.service.impl;

import com.cetc32.dh.dto.DataMenuDTO;
import com.cetc32.dh.entity.DataFile;
import com.cetc32.dh.entity.DataMenu;
import com.cetc32.dh.mybatis.DataFileMapper;
import com.cetc32.dh.mybatis.DataMenuMapper;
import com.cetc32.dh.service.DataFileService;
import com.cetc32.dh.service.DataMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cetc32.dh.entity.FileToMenu;
import com.cetc32.dh.mybatis.FileToMenuMapper;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: DataMenuServiceImpl
 * @Description:
 * @author: youqing
 * @version: 1.0
 * @date: 2020/11/21 11:04
 */
@Service
public class DataMenuServiceImpl implements DataMenuService {

    @Autowired
    public DataMenuMapper dataMenuMapper;

    @Autowired
    public DataFileMapper dataFileMapper;

    @Autowired
    public DataFileService dataFileService;

    @Autowired
    public FileToMenuMapper fileToMenuMapper;

    /**
     * 获取所有目录节点数目
     * @return Integer
     */
    @Override
    public Integer count() {
        return dataMenuMapper.countAll();
    }

    /**
     * 插入目录节点
     * @param dataMenu
     * @return Integer
     */
    @Override
    public Integer insertDataMenu(DataMenu dataMenu){
        return dataMenuMapper.insertOne(dataMenu);
    }

    /**
     * 更新目录节点
     * @param dataMenu
     * @return Integer
     */
    @Override
    public Integer updatebyId(DataMenu dataMenu) {
        return dataMenuMapper.updateById(dataMenu);
    }

    /**
     * 根据id查询目录节点
     * @param id
     * @return DataMenu
     */
    @Override
    public DataMenu queryById(Long id) {
        return dataMenuMapper.queryById(id);
    }

    /**
     * 根据id删除目录节点（单个）
     * @param id
     * @return DataMenu
     */
    @Override
    public  Integer deleteById(Long id){
        return dataMenuMapper.deleteById(id);
    }

    /**
     * 根据父节点pid查询目录节点
     * @param pid
     * @return DataMenu
     */
    @Override
    public DataMenu queryByPid(Long pid){
        return dataMenuMapper.queryByPid(pid);
    }

    /**
     * 根据父节点为id的所有目录节点
     * @param id
     * @return List<DataMenu>
     */
    @Override
    public List<DataMenu> queryByPIdSatisfyId(Long id){
        return dataMenuMapper.queryByPIdSatisfyId(id);
    }

    /**
     * 根据目录节点menuId下的所有文件
     * @param menuId
     * @return List<DataFile>
     */
    @Override
    public List<DataFile> showFilesByMenuId(Long menuId){
        List<FileToMenu> fileIds=fileToMenuMapper.selectByMenuId(menuId);
        List<DataFile> dataFileList=new ArrayList<DataFile>();
        for(int i=0;i<fileIds.size();i++){
            Long fileId=fileIds.get(i).getFileId();
            dataFileList.add(dataFileMapper.queryById(fileId));
        }
        return dataFileList;
    }

    /**
     * 查看目录节点id数（包括所有子孙节点）
     * @param id
     * @return DataMenuDTO
     */
    @Override
    public DataMenuDTO getMenuTree(Long id) {

        ArrayList<DataMenuDTO> sub = new ArrayList();
//        DataMenuDTO dataMenuDTO=new DataMenuDTO(dataMenuService.queryById(id),sub);

        DataMenu dataMenu = queryById(id);
        DataMenuDTO dataMenuDTO = new DataMenuDTO();
        BeanUtils.copyProperties(dataMenu,dataMenuDTO);

        List<DataMenu> childList = queryByPIdSatisfyId(id);
        if(childList.size()==0) {
            dataMenuDTO.setSubMenu(null);
            return dataMenuDTO;
        }
        dataMenuDTO.setSubMenu(sub);

        for (int i = 0; i < childList.size(); i++) {
            Long myId = childList.get(i).getId();
            dataMenuDTO.getSubMenu().add(getMenuTree(myId));
        }

        return dataMenuDTO;
    }

    /**
     * 根据目录节点id删除目录树（包括所有子孙节点）
     * @param id
     * @return Integer
     */
    @Override
    public Integer deleteMenuTree(Long id) {
        Integer count = 0;
        List<DataMenu> childList = queryByPIdSatisfyId(id);
        for (int i = 0; i < childList.size(); i++) {
            count += deleteMenuTree(childList.get(i).getId());
        }
        count += deleteById(id);
        return count;
    }

    /**
     * 增加目录节点
     * @param dataMenu
     * @return String
     */
    @Override
    public String addDataMenu(DataMenu dataMenu ) {
        String message = "添加成功";
        if (dataMenu.getId() == null) {
            message = "id为null,请选择正确的dataMenu！";
        } else {
            if (dataMenu.getPid() == null) {
                dataMenu.setPid(0L);
            }
            Integer count = insertDataMenu(dataMenu);
            if (count != 1) {
                message = "添加失败!";
            }
        }
            return message;
    }

    public List<DataMenu>  selectAll(){
        return dataMenuMapper.selectAll();
    }


}
